// 统一管理用户相关的接口
import request from '@/utils/request';
import { AllRoleResponseData, SetRoleData, User, UserResponseData } from './types';

// 统一接口管理
enum API {
  GET_USERS_URL = '/admin/acl/user',
  ADD_USER_URL = '/admin/acl/user/save',
  UPDATE_USER_URL = '/admin/acl/user/update',
  BATCH_DELETE_USER_URL = '/admin/acl/user/batchRemove',
  DELETE_USER_URL = '/admin/acl/user/remove',
  ALL_ROLE_URL = '/admin/acl/user/toAssign',
  ASSIGN_ROLE_URL = '/admin/acl/user/doAssignRole',
}

//获取用户接口
export const reqGetUsers = (page: number, limit: number, username: string) => {
  return request.get<any, UserResponseData>(API.GET_USERS_URL + `/${page}/${limit}/?username=${username}`);
};
//根据用户id获取角色列表
export const reqGetRoles = (id: number) => {
  return request.get<any, AllRoleResponseData>(API.ALL_ROLE_URL + `/${id}`);
};
// 添加或者修改用户
export const reqAddOrUpdateUser = (data: User) => {
  if (data.id) {
    return request.put<any, any>(API.UPDATE_USER_URL, data);
  } else {
    return request.post<any, any>(API.ADD_USER_URL, data);
  }
};
// 删除用户
export const reqDeleteUser = (id: number) => {
  return request.delete<any, any>(API.DELETE_USER_URL + `/${id}`);
};
//批量删除用户
export const reqBatchDeleteUser = (idList: number[]) => {
  return request.delete<any, any>(API.BATCH_DELETE_USER_URL, { data: idList });
};
//分配角色
export const reqAssignRole = (data: SetRoleData) => {
  return request.post<any, any>(API.ASSIGN_ROLE_URL, data);
};
